package com.merge.skarabee;


import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;


public class MainActivity extends FragmentActivity {



	FragmentManager fragmentManager = getSupportFragmentManager();

	PortInfoFragment port1;
	PortInfoFragment port2;		
	PortInfoFragment port3;		
	PortInfoFragment port4;
	
	ChartFragment chartFragment;

	ArrayList<PortInfoFragment> portFragmentList = new ArrayList<PortInfoFragment>();

	/* (non-Javadoc)
	 * @see android.app.Activity#onPostCreate(android.os.Bundle)
	 */
	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onPostCreate(savedInstanceState);
		setupView();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);		
		createFrags();
		addFragments();
	}


	private void createFrags(){
		
			port1 = new PortInfoFragment();
			port2 = new PortInfoFragment();		
			port3 = new PortInfoFragment();		
			port4 = new PortInfoFragment();
			
			portFragmentList.add(port1);
			portFragmentList.add(port2);
			portFragmentList.add(port3);
			portFragmentList.add(port4);
		
			chartFragment = new ChartFragment();
			
	}

	private void addFragments(){

		FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

		fragmentTransaction.add(R.id.p1_frame, port1);
		fragmentTransaction.add(R.id.p2_frame, port2);
		fragmentTransaction.add(R.id.p3_frame, port3);
		fragmentTransaction.add(R.id.p4_frame, port4);
		fragmentTransaction.add(R.id.rightLayout, chartFragment);

		fragmentTransaction.addToBackStack(null);
		
		fragmentTransaction.commit();
		
		getSupportFragmentManager().executePendingTransactions();
		
				

 
	}

	private void setupView(){
		
		int portNo = 1;

		for (PortInfoFragment portInfoFragment : portFragmentList) {
			portInfoFragment.setPortNumber(portNo++);
		}
		
		chartFragment.startChart();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public void onOptionsClick(View view){
		Toast toast = Toast.makeText(getApplicationContext(), "CLIIIICK", Toast.LENGTH_SHORT);
		toast.show();


	}
	
	

}	
